xarray Sandbox

For playing around during development.


In [26]:
import xarray as xr
import numpy as np
import pysd.utils as utils
import textwrap

creating dataarrays with data at instantiation


In [103]:
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
a = xr.DataArray(data=[[.1, .2],[.3,.4],[.5,.6]], coords=coords, dims=dims)
a


Out[103]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ 0.1,  0.2],
       [ 0.3,  0.4],
       [ 0.5,  0.6]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [107]:
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a, '\n'
b = xr.DataArray(data=a, coords={'Dim1': ['A', 'B', 'C']})
print b


<xarray.DataArray (Dim1: 3)>
array([3, 4, 5])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

<xarray.DataArray (Dim1: 3)>
array([3, 4, 5])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [108]:
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a, '\n'
b = xr.DataArray(data=a, coords={'Dim1': ['D', 'E', 'F']})
print b


<xarray.DataArray (Dim1: 3)>
array([3, 4, 5])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

<xarray.DataArray (Dim1: 3)>
array([3, 4, 5])
Coordinates:
  * Dim1     (Dim1) |S1 'D' 'E' 'F'

In [104]:
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a
b = xr.DataArray(data=a, coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}, dims=['Dim1', 'Dim2'])
print b


<xarray.DataArray (Dim1: 3)>
array([3, 4, 5])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C'
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-104-896e7d8ddb06> in <module>()
      1 a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
      2 print a
----> 3 b = xr.DataArray(data=a, coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}, dims=['Dim1', 'Dim2'])
      4 print b

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
    216 
    217             data = as_compatible_data(data)
--> 218             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    219             variable = Variable(dims, data, attrs, encoding, fastpath=True)
    220 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in _infer_coords_and_dims(shape, coords, dims)
     74 
     75         for d, s in zip(v.dims, v.shape):
---> 76             if s != sizes[d]:
     77                 raise ValueError('conflicting sizes for dimension %r: '
     78                                  'length %s on the data but length %s on '

KeyError: 'Dim2'

In [109]:
a = xr.DataArray(data=3, coords={'Dim1': ['A', 'B', 'C']})


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-109-86d65f98ac0c> in <module>()
----> 1 a = xr.DataArray(data=3, coords={'Dim1': ['A', 'B', 'C']})

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
    216 
    217             data = as_compatible_data(data)
--> 218             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    219             variable = Variable(dims, data, attrs, encoding, fastpath=True)
    220 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in _infer_coords_and_dims(shape, coords, dims)
     71             raise ValueError('coordinate %s has dimensions %s, but these '
     72                              'are not a subset of the DataArray '
---> 73                              'dimensions %s' % (k, v.dims, dims))
     74 
     75         for d, s in zip(v.dims, v.shape):

ValueError: coordinate Dim1 has dimensions ('Dim1',), but these are not a subset of the DataArray dimensions ()

In [40]:
subs = ['One Dimensional Subscript', 'Second Dimension Subscript', 'Depth 1']
subscript_dict = {'Second Dimension Subscript': ['Column 1', 'Column 2'], 
                  'Third Dimension Subscript': ['Depth 1', 'Depth 2'], 
                  'One Dimensional Subscript': ['Entry 1', 'Entry 2', 'Entry 3']}

coords = utils.make_coord_dict(subs, subscript_dict, terse=False)
dims = [utils.find_subscript_name(subscript_dict, sub) for sub in subs]
shape = [len(coords[dim]) for dim in dims]

text = '1,2; 3,4; 5,6'
text = text.strip(';').replace(' ', '').replace(';',',')

data = np.array([float(s) for s in text.split(',')]).reshape(shape)
datastr = np.array2string(data, separator=',').replace('\n','').replace(' ','')

datastr
a = xr.DataArray(data=data, coords=coords, dims=dims)
a

string = textwrap.dedent("""\
        xr.DataArray(data=%(datastr)s,
                     coords=%(coords)s,
                     dims=%(dims)s )""" % {
        'datastr': datastr,
        'coords': coords,
        'dims': repr(dims)
    })

eval(string)


Out[40]:
<xarray.DataArray (One Dimensional Subscript: 3, Second Dimension Subscript: 2, Third Dimension Subscript: 1)>
array([[[ 1.],
        [ 2.]],

       [[ 3.],
        [ 4.]],

       [[ 5.],
        [ 6.]]])
Coordinates:
  * Second Dimension Subscript  (Second Dimension Subscript) |S8 'Column 1' ...
  * One Dimensional Subscript   (One Dimensional Subscript) |S7 'Entry 1' ...
  * Third Dimension Subscript   (Third Dimension Subscript) |S7 'Depth 1'

In [114]:
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
val = [3, 2, 1]
a = xr.DataArray(data=np.tile(val, shape), coords=coords, dims=dims)

a


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-114-edb0a7bc07a4> in <module>()
      4 shape = [len(coords[key]) for key in dims]
      5 val = [3, 2]
----> 6 a = xr.DataArray(data=np.tile(val, shape), coords=coords, dims=dims)
      7 
      8 a

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
    216 
    217             data = as_compatible_data(data)
--> 218             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    219             variable = Variable(dims, data, attrs, encoding, fastpath=True)
    220 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in _infer_coords_and_dims(shape, coords, dims)
     77                 raise ValueError('conflicting sizes for dimension %r: '
     78                                  'length %s on the data but length %s on '
---> 79                                  'coordinate %r' % (d, sizes[d], s, k))
     80 
     81     return new_coords, dims

ValueError: conflicting sizes for dimension 'Dim2': length 4 on the data but length 2 on coordinate 'Dim2'

creating empty dataarrays


In [5]:
# using np.zeros
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.zeros(shape)*np.NaN, coords=coords, dims=dims)
a.loc[coords]


Out[5]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ nan,  nan],
       [ nan,  nan],
       [ nan,  nan]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [53]:
# using np.tile
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[coords]


Out[53]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ nan,  nan],
       [ nan,  nan],
       [ nan,  nan]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

assigning all values to array


In [ ]:


In [74]:
# assign all values to a scalar
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[coords] = 3
a.loc[coords]


Out[74]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ 3.,  3.],
       [ 3.,  3.],
       [ 3.,  3.]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [75]:
# fill all values
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[coords] = [1,2],[3,4],[5,6]
a.loc[coords]


Out[75]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [76]:
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

values = [10,20],[30,40],[50,60]
a.values = np.array(values)
a.loc[coords]


Out[76]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[10, 20],
       [30, 40],
       [50, 60]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [ ]:
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[]
a.values = np.array(values)
a.loc[coords]

In [9]:
# select a slice
a.loc[{'Dim2':['D']}]


Out[9]:
<xarray.DataArray (Dim1: 3, Dim2: 1)>
array([[10],
       [30],
       [50]])
Coordinates:
  * Dim2     (Dim2) |S1 'D'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

Assign to a slice


In [78]:
# assign to a slice (the orientation matters!)
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[{'Dim2':['D']}] = 1, 2, 3
a.loc[coords]


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-78-0862b07d5f16> in <module>()
      5 a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
      6 
----> 7 a.loc[{'Dim2':['D']}] = 1, 2, 3
      8 a.loc[coords]

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __setitem__(self, key, value)
    104 
    105     def __setitem__(self, key, value):
--> 106         self.data_array[self._remap_key(key)] = value
    107 
    108 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __setitem__(self, key, value)
    414         else:
    415             # orthogonal array indexing
--> 416             self.variable[key] = value
    417 
    418     def __delitem__(self, key):

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/variable.pyc in __setitem__(self, key, value)
    376                             'method or accessing its .values attribute.')
    377         data = orthogonally_indexable(self._data_cached())
--> 378         data[key] = value
    379 
    380     @property

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/indexing.pyc in __setitem__(self, key, value)
    360     def __setitem__(self, key, value):
    361         key = self._convert_key(key)
--> 362         self.array[key] = value
    363 
    364 

ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (1,3)

In [79]:
#assign to a slice with properly aligned array
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[{'Dim2':['D']}] = [1], [2], [3]
a.loc[coords]


Out[79]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[  1.,  nan],
       [  2.,  nan],
       [  3.,  nan]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [82]:
# assign to opposite dimension
ret = xr.DataArray(data=np.ones([3, 2])*np.NaN,
             coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']},
             dims=['Dim1', 'Dim2'] )
ret.loc[{'Dim1': ['A']}] = 50, 51
ret.loc[{'Dim1': ['B']}] = 21, 22
ret.loc[{'Dim1': ['C']}] = 3, 4
ret


Out[82]:
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ 50.,  51.],
       [ 21.,  22.],
       [  3.,   4.]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

update a slice based on an existing dataarray


In [87]:
# add a new slice
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)

a.update(new_slice)
a


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-87-eb4fc0c09274> in <module>()
      8 new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)
      9 
---> 10 a.update(new_slice)
     11 a

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/common.pyc in __getattr__(self, name)
    192                     return source[name]
    193         raise AttributeError("%r object has no attribute %r" %
--> 194                              (type(self).__name__, name))
    195 
    196     def __setattr__(self, name, value):

AttributeError: 'DataArray' object has no attribute 'update'

In [84]:
a.loc[new_coords] = new_slice


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-84-4e9fdea14323> in <module>()
----> 1 a.loc[new_coords] = new_slice

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __setitem__(self, key, value)
    104 
    105     def __setitem__(self, key, value):
--> 106         self.data_array[self._remap_key(key)] = value
    107 
    108 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __setitem__(self, key, value)
    414         else:
    415             # orthogonal array indexing
--> 416             self.variable[key] = value
    417 
    418     def __delitem__(self, key):

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/variable.pyc in __setitem__(self, key, value)
    376                             'method or accessing its .values attribute.')
    377         data = orthogonally_indexable(self._data_cached())
--> 378         data[key] = value
    379 
    380     @property

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/indexing.pyc in __setitem__(self, key, value)
    360     def __setitem__(self, key, value):
    361         key = self._convert_key(key)
--> 362         self.array[key] = value
    363 
    364 

ValueError: shape mismatch: value array of shape (1,3) could not be broadcast to indexing result of shape (3,1)

In [85]:
a.loc[new_coords].values = new_slice.loc[new_coords].values


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-85-e504098cb25d> in <module>()
----> 1 a.loc[new_coords].values = new_slice.loc[new_coords].values

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/common.pyc in __setattr__(self, name, value)
    206                     "style assignment (e.g., `ds['name'] = ...`) instead to "
    207                     "assign variables." % (name, type(self).__name__))
--> 208         object.__setattr__(self, name, value)
    209 
    210     def __dir__(self):

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in values(self, value)
    366     @values.setter
    367     def values(self, value):
--> 368         self.variable.values = value
    369 
    370     @property

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/variable.pyc in values(self, values)
    290     @values.setter
    291     def values(self, values):
--> 292         self.data = values
    293 
    294     def to_variable(self):

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/variable.pyc in data(self, data)
    247         if data.shape != self.shape:
    248             raise ValueError(
--> 249                 "replacement data must match the Variable's shape")
    250         self._data = data
    251 

ValueError: replacement data must match the Variable's shape

In [93]:
# multiplication works, why not assignment?
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
a = xr.DataArray(data=[[.1, .2],[.3,.4],[.5,.6]], coords=coords, dims=dims)

new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)

a.loc[new_coords] * new_slice.loc[new_coords]
a * new_slice
a + new_slice


Out[93]:
<xarray.DataArray (Dim1: 3, Dim2: 1)>
array([[ 1.2],
       [ 2.5],
       [ 3.8]])
Coordinates:
  * Dim2     (Dim2) object 'D'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [28]:
#a2, new_slice2 = xr.align(a,new_slice)
a.loc[new_coords]


Out[28]:
<xarray.DataArray (Dim1: 3, Dim2: 1)>
array([[1],
       [1],
       [1]])
Coordinates:
  * Dim2     (Dim2) |S1 'D'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [100]:
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
print 'Original Array:\n-----------------\n', a, '\n'

new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)
print 'New Slice:\n-----------------\n', new_slice, '\n'

ds = xr.Dataset({'a':a})
ds.update(xr.Dataset({'a':new_slice}))
print 'Updated Array:\n-----------------\n', b['a'], '\n'


Original Array:
-----------------
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ nan,  nan],
       [ nan,  nan],
       [ nan,  nan]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

New Slice:
-----------------
<xarray.DataArray (Dim2: 1, Dim1: 3)>
array([[ 1.1,  2.2,  3.3]])
Coordinates:
  * Dim2     (Dim2) |S1 'D'
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

Updated Array:
-----------------
<xarray.DataArray 'a' (Dim2: 2, Dim1: 3)>
array([[ 1.1,  2.2,  3.3],
       [ nan,  nan,  nan]])
Coordinates:
  * Dim2     (Dim2) object 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

Using array concatenation


In [2]:
m = xr.DataArray(data=1, 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
m


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-202177965ada> in <module>()
      1 m = xr.DataArray(data=1, 
----> 2                  coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
      3 m

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
    216 
    217             data = as_compatible_data(data)
--> 218             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    219             variable = Variable(dims, data, attrs, encoding, fastpath=True)
    220 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in _infer_coords_and_dims(shape, coords, dims)
     71             raise ValueError('coordinate %s has dimensions %s, but these '
     72                              'are not a subset of the DataArray '
---> 73                              'dimensions %s' % (k, v.dims, dims))
     74 
     75         for d, s in zip(v.dims, v.shape):

ValueError: coordinate Dim2 has dimensions ('Dim2',), but these are not a subset of the DataArray dimensions ()

In [ ]:


In [18]:
m = xr.DataArray(data=[1.1, 1.2, 1.3], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
n = xr.DataArray(data=[2.1, 2.2, 2.3], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-f7da549e894e> in <module>()
      1 m = xr.DataArray(data=[1.1, 1.2, 1.3], 
----> 2                  coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
      3 n = xr.DataArray(data=[2.1, 2.2, 2.3], 
      4                  coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
    216 
    217             data = as_compatible_data(data)
--> 218             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    219             variable = Variable(dims, data, attrs, encoding, fastpath=True)
    220 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataarray.pyc in _infer_coords_and_dims(shape, coords, dims)
     71             raise ValueError('coordinate %s has dimensions %s, but these '
     72                              'are not a subset of the DataArray '
---> 73                              'dimensions %s' % (k, v.dims, dims))
     74 
     75         for d, s in zip(v.dims, v.shape):

ValueError: coordinate Dim2 has dimensions ('Dim2',), but these are not a subset of the DataArray dimensions ('dim_0',)

In [19]:
m = xr.DataArray(data=[[1.1, 1.2, 1.3]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
n = xr.DataArray(data=[[2.1, 2.2, 2.3]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})

a = xr.concat([m, n], dim='Dim2')
a


Out[19]:
<xarray.DataArray (Dim2: 2, Dim1: 3)>
array([[ 1.1,  1.2,  1.3],
       [ 2.1,  2.2,  2.3]])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C'
  * Dim2     (Dim2) |S1 'D' 'E'

In [24]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})

a = xr.concat([m,n,o,p], dim=['Dim2', 'Dim3'])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-cbe53fcceda3> in <module>()
      8                  coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
      9 
---> 10 a = xr.concat([m,n,o,p], dim=['Dim2', 'Dim3'])

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in concat(objs, dim, data_vars, coords, compat, positions, indexers, mode, concat_over)
    112         raise TypeError('can only concatenate xarray Dataset and DataArray '
    113                         'objects, got %s' % type(first_obj))
--> 114     return f(objs, dim, data_vars, coords, compat, positions)
    115 
    116 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in _dataarray_concat(arrays, dim, data_vars, coords, compat, positions)
    299 
    300     ds = _dataset_concat(datasets, dim, data_vars, coords, compat,
--> 301                          positions)
    302     return arrays[0]._from_temp_dataset(ds, name)
    303 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in _dataset_concat(datasets, dim, data_vars, coords, compat, positions)
    269         insert_result_variable(k, combined)
    270 
--> 271     result = Dataset(result_vars, attrs=result_attrs)
    272     result = result.set_coords(result_coord_names)
    273 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in __init__(self, data_vars, coords, attrs, compat, **kwargs)
    207             coords = set()
    208         if data_vars is not None or coords is not None:
--> 209             self._set_init_vars_and_dims(data_vars, coords, compat)
    210         if attrs is not None:
    211             self.attrs = attrs

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in _set_init_vars_and_dims(self, vars, coords, compat)
    263         aligned = align_variables(variables)
    264         new_variables, new_coord_names = expand_variables(aligned,
--> 265                                                           compat=compat)
    266 
    267         new_coord_names.update(coords)

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/merge.pyc in expand_variables(raw_variables, old_variables, compat)
     75                     add_variable(dim, coord.variable)
     76             var = var.variable
---> 77         add_variable(name, var)
     78 
     79     return new_variables, new_coord_names

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/merge.pyc in add_variable(name, var)
     55 
     56     def add_variable(name, var):
---> 57         var = _as_dataset_variable(name, var)
     58         if name not in variables:
     59             variables[name] = var

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/merge.pyc in _as_dataset_variable(name, var)
     18             raise ValueError('the variable %r has the same name as one of its '
     19                              'dimensions %r, but it is not 1-dimensional and '
---> 20                              'thus it is not a valid index' % (name, var.dims))
     21         var = var.to_coord()
     22     return var

ValueError: the variable 'Dim2' has the same name as one of its dimensions ('concat_dim', 'Dim2'), but it is not 1-dimensional and thus it is not a valid index

In [107]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.align(m,n,o,p, join='outer')
xr.concat(a, 'Dim1')


Out[107]:
<xarray.DataArray (Dim2: 2, Dim3: 2, Dim1: 12)>
array([[[ 1.1,  1.2,  1.3,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
          nan],
        [ nan,  nan,  nan,  nan,  nan,  nan,  3.1,  3.2,  3.3,  nan,  nan,
          nan]],

       [[ nan,  nan,  nan,  2.1,  2.2,  2.3,  nan,  nan,  nan,  nan,  nan,
          nan],
        [ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  4.1,  4.2,
          4.3]]])
Coordinates:
  * Dim2     (Dim2) object 'D' 'E'
  * Dim3     (Dim3) object 'F' 'G'
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 'A' 'B' 'C' 'A' 'B' 'C' 'A' 'B' 'C'

In [60]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.align(m,n,o,p, join='outer')
xr.concat(a, dim=['Dim2', 'Dim3'])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-a47ea6f1b63b> in <module>()
      8                  coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
      9 a = xr.align(m,n,o,p, join='outer')
---> 10 xr.concat(a, dim=['Dim2', 'Dim3'])

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in concat(objs, dim, data_vars, coords, compat, positions, indexers, mode, concat_over)
    112         raise TypeError('can only concatenate xarray Dataset and DataArray '
    113                         'objects, got %s' % type(first_obj))
--> 114     return f(objs, dim, data_vars, coords, compat, positions)
    115 
    116 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in _dataarray_concat(arrays, dim, data_vars, coords, compat, positions)
    299 
    300     ds = _dataset_concat(datasets, dim, data_vars, coords, compat,
--> 301                          positions)
    302     return arrays[0]._from_temp_dataset(ds, name)
    303 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in _dataset_concat(datasets, dim, data_vars, coords, compat, positions)
    274     if coord is not None:
    275         # add concat dimension last to ensure that its in the final Dataset
--> 276         result[coord.name] = coord
    277 
    278     return result

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in __setitem__(self, key, value)
    545             raise NotImplementedError('cannot yet use a dictionary as a key '
    546                                       'to set Dataset values')
--> 547         self.update({key: value})
    548 
    549     def __delitem__(self, key):

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in update(self, other, inplace)
   1441         """
   1442         return self.merge(
-> 1443             other, inplace=inplace, overwrite_vars=list(other), join='left')
   1444 
   1445     def merge(self, other, inplace=False, overwrite_vars=set(),

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in merge(self, other, inplace, overwrite_vars, compat, join)
   1492             self, other, overwrite_vars, compat=compat, join=join)
   1493         obj = self if inplace else self.copy()
-> 1494         obj._update_vars_and_coords(replace_vars, new_coord_names)
   1495         return obj
   1496 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in _update_vars_and_coords(self, new_variables, new_coord_names, needs_copy, check_coord_names)
    242 
    243         variables.update(new_variables)
--> 244         dims = _calculate_dims(variables)
    245         # all checks are complete: it's safe to update
    246         self._variables = variables

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in _calculate_dims(variables)
     84                 raise ValueError('conflicting sizes for dimension %r: '
     85                                  'length %s on %r and length %s on %r' %
---> 86                                  (dim, size, k, dims[dim], last_used[dim]))
     87     return dims
     88 

ValueError: conflicting sizes for dimension 'concat_dim': length 2 on 'concat_dim' and length 4 on <this-array>

In [101]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})

def xrmerge(das, accept_new=True):
    da = das[0]
    for new_da in das[1:]:
        # Expand both to have same dimensions, padding with NaN
        da, new_da = xr.align(da, new_da, join='outer')
        # Fill NaNs one way or the other re. accept_new
        da = new_da.fillna(da) if accept_new else da.fillna(new_da)
    return da

a = xrmerge([m,n,o,p])
a


Out[101]:
<xarray.DataArray (Dim2: 2, Dim3: 2, Dim1: 3)>
array([[[ 1.1,  1.2,  1.3],
        [ 3.1,  3.2,  3.3]],

       [[ 2.1,  2.2,  2.3],
        [ 4.1,  4.2,  4.3]]])
Coordinates:
  * Dim2     (Dim2) object 'D' 'E'
  * Dim3     (Dim3) object 'F' 'G'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [114]:
a.loc[{'Dim1':'A'}]


Out[114]:
<xarray.DataArray (Dim2: 2, Dim3: 2)>
array([[ 1.1,  3.1],
       [ 2.1,  4.1]])
Coordinates:
  * Dim2     (Dim2) object 'D' 'E'
  * Dim3     (Dim3) object 'F' 'G'
    Dim1     |S1 'A'

In [115]:
a.loc[{'Dim2':'D'}]


Out[115]:
<xarray.DataArray (Dim3: 2, Dim1: 3)>
array([[ 1.1,  1.2,  1.3],
       [ 3.1,  3.2,  3.3]])
Coordinates:
    Dim2     |S1 'D'
  * Dim3     (Dim3) object 'F' 'G'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [116]:
a.loc[{'Dim3':'F'}]


Out[116]:
<xarray.DataArray (Dim2: 2, Dim1: 3)>
array([[ 1.1,  1.2,  1.3],
       [ 2.1,  2.2,  2.3]])
Coordinates:
  * Dim2     (Dim2) object 'D' 'E'
    Dim3     |S1 'F'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

In [117]:
data =[[[ 1.1,  1.2,  1.3],
        [ 3.1,  3.2,  3.3]],

       [[ 2.1,  2.2,  2.3],
        [ 4.1,  4.2,  4.3]]]

b = xr.DataArray(data=data, 
             coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D', 'E'], 'Dim3':['F', 'G']})

In [126]:
b.values


Out[126]:
array([[[ 1.1,  1.2,  1.3],
        [ 3.1,  3.2,  3.3]],

       [[ 2.1,  2.2,  2.3],
        [ 4.1,  4.2,  4.3]]])

In [136]:
for i in b.coords.iteritems():
    print i[0], i[1].values

{key:list(value.values) for key, value in b.coords.iteritems()}


Dim2 ['D' 'E']
Dim3 ['F' 'G']
Dim1 ['A' 'B' 'C']
Out[136]:
{'Dim1': ['A', 'B', 'C'], 'Dim2': ['D', 'E'], 'Dim3': ['F', 'G']}

In [ ]:


In [131]:
for i in b.indexes.iteritems():
    print i[0], i[1].values


Dim2 ['D' 'E']
Dim3 ['F' 'G']
Dim1 ['A' 'B' 'C']

In [82]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
ds = xr.Dataset({'m':m,'n':n,'o':o,'p':p})
ds.to_array()


Out[82]:
<xarray.DataArray (variable: 4, Dim2: 2, Dim3: 2, Dim1: 3)>
array([[[[ nan,  nan,  nan],
         [ nan,  nan,  nan]],

        [[ nan,  nan,  nan],
         [ 4.1,  4.2,  4.3]]],


       [[[ 1.1,  1.2,  1.3],
         [ nan,  nan,  nan]],

        [[ nan,  nan,  nan],
         [ nan,  nan,  nan]]],


       [[[ nan,  nan,  nan],
         [ 3.1,  3.2,  3.3]],

        [[ nan,  nan,  nan],
         [ nan,  nan,  nan]]],


       [[[ nan,  nan,  nan],
         [ nan,  nan,  nan]],

        [[ 2.1,  2.2,  2.3],
         [ nan,  nan,  nan]]]])
Coordinates:
  * Dim2      (Dim2) object 'D' 'E'
  * Dim3      (Dim3) object 'F' 'G'
  * Dim1      (Dim1) |S1 'A' 'B' 'C'
  * variable  (variable) |S1 'p' 'm' 'o' 'n'

In [85]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
l = [n,o,p]
for j in l:
    xr.concat([m, j])
    break


/Users/houghton/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:11: FutureWarning: the `dim` argument to `concat` will be required in a future version of xarray; for now, setting it to the old default of 'concat_dim'
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-85-55823056d8a0> in <module>()
      9 l = [n,o,p]
     10 for j in l:
---> 11     xr.concat([m, j])
     12     break

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in concat(objs, dim, data_vars, coords, compat, positions, indexers, mode, concat_over)
    112         raise TypeError('can only concatenate xarray Dataset and DataArray '
    113                         'objects, got %s' % type(first_obj))
--> 114     return f(objs, dim, data_vars, coords, compat, positions)
    115 
    116 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in _dataarray_concat(arrays, dim, data_vars, coords, compat, positions)
    299 
    300     ds = _dataset_concat(datasets, dim, data_vars, coords, compat,
--> 301                          positions)
    302     return arrays[0]._from_temp_dataset(ds, name)
    303 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/combine.pyc in _dataset_concat(datasets, dim, data_vars, coords, compat, positions)
    269         insert_result_variable(k, combined)
    270 
--> 271     result = Dataset(result_vars, attrs=result_attrs)
    272     result = result.set_coords(result_coord_names)
    273 

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in __init__(self, data_vars, coords, attrs, compat, **kwargs)
    207             coords = set()
    208         if data_vars is not None or coords is not None:
--> 209             self._set_init_vars_and_dims(data_vars, coords, compat)
    210         if attrs is not None:
    211             self.attrs = attrs

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in _set_init_vars_and_dims(self, vars, coords, compat)
    263         aligned = align_variables(variables)
    264         new_variables, new_coord_names = expand_variables(aligned,
--> 265                                                           compat=compat)
    266 
    267         new_coord_names.update(coords)

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/merge.pyc in expand_variables(raw_variables, old_variables, compat)
     75                     add_variable(dim, coord.variable)
     76             var = var.variable
---> 77         add_variable(name, var)
     78 
     79     return new_variables, new_coord_names

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/merge.pyc in add_variable(name, var)
     55 
     56     def add_variable(name, var):
---> 57         var = _as_dataset_variable(name, var)
     58         if name not in variables:
     59             variables[name] = var

/Users/houghton/anaconda/lib/python2.7/site-packages/xarray/core/merge.pyc in _as_dataset_variable(name, var)
     18             raise ValueError('the variable %r has the same name as one of its '
     19                              'dimensions %r, but it is not 1-dimensional and '
---> 20                              'thus it is not a valid index' % (name, var.dims))
     21         var = var.to_coord()
     22     return var

ValueError: the variable 'Dim2' has the same name as one of its dimensions ('concat_dims', 'Dim2'), but it is not 1-dimensional and thus it is not a valid index

In [ ]:
base = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                    coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})

in pandas


In [41]:
import pandas as pd
df = pd.DataFrame(index=range(5), columns=['a','b','c','d'])
df2 = pd.DataFrame(index=range(3), columns=['a'], data=range(3))
df.update(df2)
df


Out[41]:
a b c d
0 0 NaN NaN NaN
1 1 NaN NaN NaN
2 2 NaN NaN NaN
3 NaN NaN NaN NaN
4 NaN NaN NaN NaN

In [46]:
df.loc[2:4, 'b'] = 5
df


Out[46]:
a b c d
0 0 NaN NaN NaN
1 1 NaN NaN NaN
2 2 5 NaN NaN
3 NaN 5 NaN NaN
4 NaN 5 NaN NaN

df.loc[2:4, 'c'] = [5,6,7] df

Trying all the types of functions


In [58]:
upstream_funcs = [
    lambda: 5
    lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C']})
    lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
    lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D', 'E']})
]

In [59]:



Out[59]:
a b c d
0 0 NaN NaN NaN
1 1 NaN NaN NaN
2 2 NaN NaN NaN
3 NaN NaN NaN NaN
4 NaN NaN NaN NaN

In [68]:
a.coords.values()


Out[68]:
[<xarray.DataArray 'Dim2' (Dim2: 2)>
 array(['D', 'E'], dtype=object)
 Coordinates:
   * Dim2     (Dim2) object 'D' 'E', <xarray.DataArray 'Dim3' (Dim3: 2)>
 array(['F', 'G'], dtype=object)
 Coordinates:
   * Dim3     (Dim3) object 'F' 'G', <xarray.DataArray 'Dim1' (Dim1: 3)>
 array(['A', 'B', 'C'], 
       dtype='|S1')
 Coordinates:
   * Dim1     (Dim1) |S1 'A' 'B' 'C']

In [73]:
{key:list(val.values) for key, val in a.coords.iteritems()}


Out[73]:
{'Dim1': ['A', 'B', 'C'], 'Dim2': ['D', 'E'], 'Dim3': ['F', 'G']}

In [ ]: